#!/bin/bash

# Under testing, may not work as expected

# Run pgrok with something like this:
#   pgrok 80
# or
#   pgrok -subdomain=customsubdomain 80
#
# Then run this script and it should output the current pgrok url.
# This is extremely useful when you're not using subdomain and you cannot guess
# what's the URL going to be. You can use either one of these below depending
# on your usecase. jq might not be installed on every computer, so I'd choose
# the second one.

# with jq:
( wget localhost:4040/api/tunnels -q -O - || curl -s localhost:4040/api/tunnels ) | sed -n "s/^.*window.data = JSON.parse(\"\(.*\)\");.*$/\1/p" | sed 's/\\"/"/g' | jq -r '.UiState.Tunnels[1].PublicUrl'

# without jq:
( wget localhost:4040/api/tunnels -q -O - || curl -s localhost:4040/api/tunnels ) | sed -n "s/^.*\(https:\/\/.*\.ejemplo\.me\).*$/\1/p"
